//
//  MOUser.m
//  MobileOffice
//
//  Created by 冰冰 on 2019/6/19.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOUser.h"

@implementation MOUser

+ (instancetype)sharedMOUser
{
    return [[self alloc]sharedMOUser];
}

+ (instancetype)initUserWithDict:(NSDictionary *)dict
{
    return [[self alloc] initUserWithDict:dict];
}

+ (void)updateStringValue:(NSString *)strValue forKey:(NSString *)key
{
    return [[self alloc] updateStringValue:strValue forKey:key];
}

+ (instancetype)initUserWithStr:(NSString *)string withType:(NSString *)type{
    return [[self alloc] initUserWithString:string withType:type];
}

- (instancetype)sharedMOUser
{
    MOUser *user = [[MOUser alloc] init];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    user.org_id = [userDefaults valueForKey:@"USER_NEW_ORGID"];
    user.email = [userDefaults valueForKey:@"USER_NEW_EMAIL"];
    user.org_name = [userDefaults valueForKey:@"USER_NEW_ORGNAME"];
    user.phone = [userDefaults valueForKey:@"USER_NEW_PHONE"];
    user.msg = [userDefaults valueForKey:@"USER_NEW_MSG"];
    user.user_id = [userDefaults valueForKey:@"USER_NEW_USERID"];
    user.userName = [userDefaults valueForKey:@"USER_NEW_USERNAME"];
    user.password = [userDefaults valueForKey:@"USER_NEW_PASSWORD"];
    user.token = [userDefaults valueForKey:@"USER_NEW_TOKEN"];
    user.code = [userDefaults valueForKey:@"USER_NEW_CODE"];
    user.rolenumbers = [userDefaults valueForKey:@"USER_NEW_ROLENUMBERS"];
    user.role_id = [userDefaults valueForKey:@"USER_NEW_ROLEID"];
    user.name = [userDefaults valueForKey:@"USER_NEW_NAME"];
    user.status = [userDefaults valueForKey:@"USER_NEW_STATUS"];
    user.openGesturePassword = [userDefaults valueForKey:@"USER_NEW_OPENGESTUREPASSWORD"];
    user.haveGesturePassword = [userDefaults valueForKey:@"USER_NEW_HAVEGESTUREPASSWORD"];
    
    
    return user;
}



- (instancetype)initUserWithDict:(NSDictionary *)dict
{
    if (self = [super init])
    {
    
        //用户基本数据（待确定）
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        if (dict[@"org_id"]) {
            [userDefaults setValue:[NSString passNullForString:dict[@"org_id"]] forKey:@"USER_NEW_ORGID"];
        }
        
        if (dict[@"email"]) {
            [userDefaults setValue:[NSString passNullForString:dict[@"email"]] forKey:@"USER_NEW_EMAIL"];
        }
        
        if (dict[@"org_name"]) {
            [userDefaults setValue:[NSString passNullForString:dict[@"org_name"]] forKey:@"USER_NEW_ORGNAME"];
        }
        
        if (dict[@"phone"]) {
            [userDefaults setValue:[NSString passNullForString:dict[@"phone"]] forKey:@"USER_NEW_PHONE"];
        }
        
        if (dict[@"msg"]) {
            [userDefaults setValue:[NSString passNullForString:dict[@"msg"]] forKey:@"USER_NEW_MSG"];
        }
        
        if (dict[@"user_id"]) {
            [userDefaults setValue:[NSString passNullForString:dict[@"user_id"]] forKey:@"USER_NEW_USERID"];
        }
        
        if (dict[@"userName"]) {
            [userDefaults setValue:[NSString passNullForString:dict[@"userName"]] forKey:@"USER_NEW_USERNAME"];
        }
        
        if (dict[@"password"]) {
            [userDefaults setValue:[NSString passNullForString:dict[@"password"]] forKey:@"USER_NEW_PASSWORD"];
        }
        
        if (dict[@"token"]) {
            [userDefaults setValue:[NSString passNullForString:dict[@"token"]] forKey:@"USER_NEW_TOKEN"];
        }
        
        if (dict[@"code"]) {
            [userDefaults setValue:[NSString passNullForString:dict[@"code"]] forKey:@"USER_NEW_CODE"];
        }
        
        if (dict[@"rolenumbers"]) {
            [userDefaults setValue:[NSString passNullForString:dict[@"rolenumbers"]] forKey:@"USER_NEW_ROLENUMBERS"];
        }
        
        if (dict[@"role_id"]) {
            [userDefaults setValue:[NSString passNullForString:dict[@"role_id"]] forKey:@"USER_NEW_ROLEID"];
        }
        
        if (dict[@"name"]) {
            [userDefaults setValue:[NSString passNullForString:dict[@"name"]] forKey:@"USER_NEW_NAME"];
        }
        
        if (dict[@"status"]) {
            [userDefaults setValue:[NSString passNullForString:dict[@"status"]] forKey:@"USER_NEW_STATUS"];
        }

        [userDefaults synchronize];
    }
    return self;
}

//存储其他一些数据
- (instancetype)initUserWithString:(NSString *)string withType:(NSString *)type
{
    if (self = [super init])
    {
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        if ([type isEqualToString:@"deviceToken"]){
            [userDefaults setValue:string forKey:@"USER_DEVICETOKEN"];
        }
        if ([type isEqualToString:@"user_usKey"]){//登录失败 清除用户信息后再存储 自动登录用
            [userDefaults setValue:string forKey:@"US_KEY"];
        }
        if ([type isEqualToString:@"firstCome"]) {
            [userDefaults setValue:string forKey:@"FIRST_COME"];
        }
        if ([type isEqualToString:@"userPhotoUrl"]) {
            [userDefaults setValue:string forKey:@"USER_NEW_HEAD_URL"];
        }
        if ([type isEqualToString:@"changeNikeName"]) {
            [userDefaults setValue:string forKey:@"NICK_NEW_NAME"];
        }
        if ([type isEqualToString:@"applyCerNikeName"]) {
            [userDefaults setValue:string forKey:@"USER_ISCERTIFICATION"];
        }
        [userDefaults synchronize];
    }
    return self;
}

- (void)updateStringValue:(NSString *)strValue forKey:(NSString *)key
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setValue:strValue forKey:key];
    [userDefaults synchronize];
}

+ (void)clearUser
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *appDomain = [[NSBundle mainBundle]bundleIdentifier];
    [userDefaults removePersistentDomainForName:appDomain];
}


@end
